<?php
class Application_Model_DbTable_FeedTag extends CF_DbTable_Table
{
	protected $_name = 'feeds_tags';
	protected $_columns = array(
								'tag_id'  => null,
								'feed_id' => null,
								'number'  => null,
								'link_id' => null
								);



	/**
	 * check if there a record with
	 * such combination of feed and tag
	 * argument is concatination of the feed_id and the tag_id
	 */

	public function hasDefeniteCombinationOfFeedTag($feed_idtag_id = null)
	{
		$select = $this->getAdapter()->select()->
									 from('feeds_tags',
									 array('combination' =>'CONCAT(feed_id,tag_id)'))->
									 where('CONCAT(feed_id,tag_id)=?',$feed_idtag_id);
		$resources = $this->getAdapter()->query($select);

		$result = $resources->rowCount();
		if($result > 0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * getTagsCloud()
	 *
	 * get tag occurances grouped by link_id
	 * @param $link_id
	 */
	public function getTagsCloud($link_id = null)
	{
		$select = $this->getAdapter()->select()
				  					 ->from($this->_name,
				  					 	array('general_number'=>'COUNT(number)','tag_id','link_id'));
		if(!is_null($link_id)){
	 		$select->where('link_id=?',$link_id);
	 	}		
	 	$select->join('tags',
	 		$this->_name.'.tag_id=tags.id',
	 			array('tag'=>'title'))
	 	->group('tag_id');
	 		
		$resources = $this->getAdapter()->query($select);
		return $resources->fetchAll();
	}


}